package cn.topevery.garden.dal.logic;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjAuditDao;
import cn.topevery.garden.entity.dto.ProjAuditDTO;
import cn.topevery.garden.entity.po.ProjAudit;
import cn.topevery.garden.entity.po.ProjBasicInfo;
import cn.topevery.garden.entity.po.ProjFiles;
import cn.topevery.garden.framework.Database;
import cn.topevery.garden.framework.base.BaseLogic;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 审计决算表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Service
public class ProjAuditLogic extends BaseLogic<ProjAudit> {

    @Autowired
    private Mapper mapper;

    @Autowired
    ProjAuditDao projAuditDao;

    @Autowired
    ProjBasicInfoLogic projBasicInfoLogic;

    @Autowired
    ProjFilesLogic projFilesLogic;

    @Override
    public BaseEntityDao<ProjAudit, Integer> getDao() {
        return projAuditDao;
    }

    /**
     * 新增/修改
     *
     * @param userId
     * @param dto
     */
    public void insertOrUpdate(String userId, ProjAuditDTO dto) {
        Database.transactionScope(() -> {
            if (StringUtils.isBlank(dto.getProjAuditId())) {
                dto.setProjAuditId(UUID.randomUUID().toString());
            }
            //如果是编辑，可能会删除一些条目
            if (CollectionUtils.isNotEmpty(dto.getDeleteObjIds())) {
                delete(userId, dto.getDeleteObjIds().toArray(new Integer[0]));
            }
            for (ProjAudit projAudit : dto.getProjAuditList()) {
                projAudit.setProjId(dto.getProjId());
                projAudit.setProjAuditId(dto.getProjAuditId());
                if (ObjectUtil.isEmpty(projAudit.getId())) {
                    insert(userId, projAudit);
                } else {
                    update(userId, projAudit);
                }
            }
            //  保存附件
            if (ObjectUtil.isNotEmpty(dto.getFiles())) {
                dto.getFiles().stream().forEach(i -> {
                    i.setProjId(dto.getProjId());
                    i.setProjStageId(dto.getProjAuditId());
                });
                projFilesLogic.insert(userId, dto.getFiles());
            }
            //  删除附件
            if (CollectionUtils.isNotEmpty(dto.getDeleteFileIds())) {
                SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_proj_files")
                        .setColumnValue(true, "c_db_status", 1);
                sqlUpdateMate.where(true, "c_proj_stage_id", ScriptConditionEnum.Eq, dto.getProjAuditId())
                        .where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                        .where(true, "c_file_id", ScriptConditionEnum.In, dto.getDeleteFileIds());
                projFilesLogic.updateSqlUpdateMate(sqlUpdateMate);
            }
        });
    }

    /**
     * 根据projId获得详情
     *
     * @param projId
     * @return
     */
    public ProjAuditDTO getByProjId(String projId) {
        List<ProjAudit> projAuditList = projAuditDao.getListEntityByColumns(new ArrayList<KeyValue>(2) {{
            add(new KeyValue("c_db_status", 0));
            add(new KeyValue("c_proj_id", projId));
        }});
        if (CollectionUtils.isEmpty(projAuditList)) {
            return null;
        }
        ProjAuditDTO projAuditDTO = new ProjAuditDTO();
        String projAuditId = projAuditList.get(0).getProjAuditId();
        projAuditDTO.setProjAuditId(projAuditId);
        List<ProjFiles> files = projFilesLogic.getListEntityByColumns(new ArrayList<KeyValue>(3) {{
            add(new KeyValue("c_db_status", 0));
            add(new KeyValue("c_proj_id", projId));
            add(new KeyValue("c_proj_stage_id", projAuditId));
        }});
        projAuditDTO.setFiles(files);
        ProjBasicInfo basicInfo = projBasicInfoLogic.getByProjId(projId);
        projAuditDTO.setProjBasicInfo(basicInfo);
        projAuditDTO.setProjAuditList(projAuditList);
        return projAuditDTO;
    }
}
